//
//  setTextVC.m
//  tcpoctest
//
//  Created by gump on 11/7/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "setTextVC.h"
#import "WFLabel_blackShadow.h"
#import "PublicFun.h"
#import "netManager.h"
#import "DataControl.h"
#import "ViewManager.h"

@interface setTextVC ()

@end

@implementation setTextVC

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = @"修改昵称";
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回" target:self action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    //[backItem release];
    
    UIBarButtonItem *modifyItem = [self createBarButtonItemWithTitle:@"提交" target:self action:@selector(modifyButtonPressed)];
    self.navItem.rightBarButtonItem = modifyItem;
    //[modifyItem release];
    
    UIImageView *inputBgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 55, 300, 44)];
    inputBgView.image = [UIImage imageNamed:@"cell.png"];
    inputBgView.userInteractionEnabled = YES;
    [self.view addSubview:inputBgView];
    //[inputBgView release];
    
    inputTF = [[UITextField alloc]initWithFrame:CGRectMake(5, 0, 290, 44)];
    inputTF.font = CENTER_CELL_CONTENT_FONT;
    inputTF.textColor=[UIColor colorWithWhite:0.4 alpha:1.0];
    inputTF.borderStyle=UITextBorderStyleNone;
    inputTF.leftViewMode = UITextFieldViewModeAlways;
    inputTF.placeholder = @"请输入昵称";
    inputTF.text = [DataControl shareControl].personDS.uinfo.name;
    inputTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    inputTF.delegate =self;
    [inputBgView addSubview:inputTF];
}

#pragma mark -private fun-
-(void)backButtonPressed
{
    inputTF.text = @"";
    
    [[ViewManager sharedViewManager] backView:self];
}

-(void)modifyButtonPressed
{
    if ([inputTF isFirstResponder]) {
        [inputTF resignFirstResponder];
    }
    
    if ([self CheckInput])
    {
        
        [[DataControl shareControl] SetChangeName:inputTF.text];
        [[DataControl shareControl].LCDS addLogicCommand:LC_SETNAME];
        
        [DataControl shareControl].personDS.uinfo.name = inputTF.text;
        
        [[DataControl shareControl] SetUpdateUserType:UUT_NAME];
        [[DataControl shareControl].LCDS addLogicCommand:LC_UPDATEUIDB];
        
        
        FirendInfo *finfo = [[DataControl shareControl] SearchInfo:
                             [DataControl shareControl].personDS.uinfo.mail];
        if (finfo != nil) {
            finfo.name = inputTF.text;
        }
        
        inputTF.text = @"";
        
        [[ViewManager sharedViewManager] backView:self];
        
    }
}

-(bool)CheckInput
{
    bool bright = true;
    
    do {
        if([inputTF.text length] == 0)
        {
            [[ViewManager sharedViewManager] showFailHUDWithStatus:@"请输入新昵称"
                                                         withDelay:2
                                                          withView:self.view];
            
            bright = false;
            break;
        }
        
        if ([inputTF.text length] > NAME_LIMITE_LENGTH)
        {
            [[ViewManager sharedViewManager] showFailHUDWithStatus:@"昵称太长耶"
                                                         withDelay:2
                                                          withView:self.view];
            
            bright = false;
            break;
        }
        
    } while (false);
    
    return bright;
}

#pragma mark -textfield delegate-
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self modifyButtonPressed];
    
    return true;
}

@end
